package com.kdzy.lshy.socity_server.repository;


import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

@Repository
public class BaseDao extends JdbcTemplate {

    public BaseDao(DataSource dataSource) {

        super(dataSource);
    }

    public Long getCount(String sql) {
        return (Long) queryForList(sql).get(0).get("count");
    }
    public Long getCount(String sql, List<Object> tags) {
        return (Long) queryForList(sql,tags.toArray()).get(0).get("count");
    }
    //根据条件查询所有结果
    public List<Map<String, Object>> getAllByCondition(String sql) {

        return queryForList(sql);

    }

    //直接添加
    public void addOne(String sql) {

        execute(sql);

    }

    //更新
    public void updateOne(String sql) {
        update(sql);
    }

    //删除
    public void deleteOnebyID(String sql) {
        execute(sql);
    }

    //获取最新的主键
    public int getLastID(String sql) {
        int last = queryForObject(sql, Integer.class, new Object[]{});
        return last;
    }

    //普通执行一句SQL
    public void executeSQL(String sql) {

        execute(sql);
    }

    public String selectByID(String sql) {
        return (String) queryForList(sql).get(0).get("version");
    }

}
